from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from time import sleep
from random import choice


# 外挂生成随机时间
time_list = [
    1.56, 1.73, 1.35, 1.92, 1.01,1.88, 1.52, 1.96, 1.98, 1.17, 1.67,1.32,1.53,1.71,1.79,1.26, 1.08, 1.58, 1.33, 1.26
]


# 初始化selenium机器人对象，并且进入网站，多种隐藏手段
service = Service(r"D:\WPS Cloud Files\edgedriver_win64\msedgedriver.exe")
opt = Options()
opt.add_argument("--disable-blink-features=AutomationControlled")
opt.add_argument("--disable-extensions")
opt.add_argument("--no-sandbox")
opt.add_experimental_option("detach",True)
opt.add_argument("--disable-dev-shm-usage")
opt.add_experimental_option("excludeSwitches",["enable-automation"])
opt.add_experimental_option("useAutomationExtension",False)
browser = webdriver.Edge(service=service,options=opt)
browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument",{
    "source":'''
    Object.defineProperty(navigator,'webdriver',{
        get:() => undefined
    })
    ''' })
browser.maximize_window()

# 使用stealth.min.js,先运行一遍，这是真行还是假行？？？
with open(r"D:\Python_Projects\spider_labs\Scraping_Resources\stealth.min.js"
          ,'r') as js:
    stealth = js.read()
browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument",
                        {
                            "source":stealth
                        })

# 初始化动作链对象
ac = ActionChains(browser)

# 进入实验室安全入口
browser.get("https://sysaq.sdu.edu.cn/lab-study-front/home")

# 点击登录输入账号密码
log_in = browser.find_element(By.XPATH,"/html/body/div[1]/div[1]/div/header/div/div[1]/div[2]/div[2]/div[1]/div")
sleep(choice(time_list)+0.5)
log_in.click()

# 同一身份认证
sleep(choice(time_list))
uniform = browser.find_element(By.XPATH,'//*[@id="wrapApp"]/div/div/div/div/form/div/div/div/a')
uniform.click()

# 第一登录流程
sleep(choice(time_list))
user_name = browser.find_element(By.XPATH,'//*[@id="un"]')
password = browser.find_element(By.XPATH,'//*[@id="pd"]')
user_name.send_keys("202500130030")
sleep(choice(time_list))
password.send_keys("rogeaustine_123456")
sleep(choice(time_list))
confirm = browser.find_element(By.XPATH,'//*[@id="index_login_btn"]')
sleep(choice(time_list))
confirm.click()

# 点击微课堂  /html/body/div[1]/div[1]/div/header/div/div[2]/div/ul/a[5]
sleep(choice(time_list))
tiny_class = browser.find_element(By.XPATH,'/html/body/div[1]/div[1]/div/header/div/div[2]/div/ul/a[5]')
tiny_class.click()

# 将所有课程数组加入队列
course_queue = [f'//*[@id="course"]/div/div[2]/div[{i}]/div/div/div[3]/button'
                for i in range(1,10)]

for item in course_queue:
    print(item)
    sleep(choice(time_list))
    course = browser.find_element(By.XPATH,item)
    sleep(choice(time_list))

    course.click()
    sleep(4)
    # /html/body/div[1]/div[1]/div/div[2]/div/div[2]/ul/li/ul/li[1]/div[2]/div/span
    # /html/body/div[1]/div[1]/div/div[2]/div/div[2]/ul/li/ul/li[1]/div[2]/button/span
    try:
        # 一切正常进入看课, 需要用到动作链
        start_lesson = browser.find_element(
            By.XPATH,
    "/html/body/div[1]/div[1]/div/div[2]/div/div[2]/ul/li/ul/li[1]/div[2]/div/span"
        )
        ac.move_to_element(start_lesson)
        ac.pause(choice(time_list))
        ac.click()
        ac.pause(choice(time_list))
        sleep(100)
    except:
        # 当出现错误，或者看完时，直接跳出？？？
        # 其实应该browser.back()???
        continue

"""事实证明，问题出自于这里，为什么一到遍历，就不行呢"""
browser.quit()

